﻿using System;
using System.Configuration;
using Microsoft.Owin.Hosting;
using Sustainalytics.DataLayer.ConnectionStrings;
using Sustainalytics.Entities.ConnectionStrings;
using Sustainalytics.Utils;


namespace Sustainalytics.CarbonData.Service
{
    /// <summary>
    /// Console app startup.
    /// </summary>
    public static class ProgramConsole
    {        
        public static void Start(string[] args)
        {
            if (args.Length == 1)
            {
                try
                {
                    if (args[0] == "sync")
                    {
                        var connectionString = ConfigurationManager.ConnectionStrings["ClientPlatform"].ConnectionString;
                        var connectionStringsManager =
                            ConnectionStringsManager.GetInstance(ConnectionStringsRepository.GetInstance(connectionString));
                        var connectionString2 = connectionStringsManager.GetConnectionString(ConnectionStringType.ClientDW);
                        
                        //var blLayer = ProductInvolvementBussinesLogic.CreateProductInvolvementBussinesLogic(connectionString, connectionString2, 
                        //                            ProductInvolvementEventSource.Log);

                        //blLayer.Start(null);

                        //Console.WriteLine(blLayer.Report());
                    }
                }
                catch (Exception ex)
                {
                    CommonEventSource.Log.Exception(ex);
                    Console.WriteLine(ex.ToString());
                }
            }
            else
            {
                try
                {
                    var url = HostConfig.GetHostUrl();
                    using (WebApp.Start<Startup>(url))
                    {
                        Console.WriteLine("Started on: " + url);
                        Console.ReadKey();
                    }
                }
                catch (Exception ex)
                {
                    CommonEventSource.Log.Exception(ex);
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                    Console.ReadKey();
                }
            }
        }
    }
}
